<template>
  <div v-if="dataed">
    <el-drawer v-if="dataed.formType == 'drawForm'" :size="dataed.superFormOpt?.width" destroy-on-close v-model="dataed.open" direction="rtl">
      <template #header>
        <h4>{{ dataed.superFormOpt?.title || "" }}</h4>
      </template>
      <template #default>
        <HfForm :ref="(el: any) => (dataed!.formRef = el)" v-on="formDate.formEmitsObj" v-bind="formDate.formPropsObj">
          <template #otherItem="{ item, key }"><slot name="otherItem" :key="key" :item="item"></slot> </template>
          <template #otherFormItem="{ form }"><slot name="otherFormItem" :form="form"></slot> </template>
          <template #otherBtn="data"><slot name="otherBtn" :data="data"></slot></template>
          <template #recycleName="data"><slot name="recycleName" v-bind="data"></slot></template>
        </HfForm>
      </template>
      <template #footer>
        <div style="flex: auto">
          <el-button @click="dataed.open = false">关闭</el-button>
          <el-button v-if="!dataed.superFormOpt?.noSubmit" type="primary" @click="dataed.methods!.submitFormFn" :loading="dataed.subLoading" :disabled="dataed.subLoading">确 定</el-button>
          <slot name="otherFoterBtn"></slot>
        </div>
      </template>
    </el-drawer>
    <el-dialog :title="dataed.superFormOpt?.title || ''" v-else-if="dataed.formType == 'diologForm'" v-model="dataed.open" :top="dataed.superFormOpt?.top || ''" :width="dataed.superFormOpt?.width || '500px'" append-to-body>
      <HfForm :ref="(el: any) => (dataed!.formRef = el)" v-on="formDate.formEmitsObj" v-bind="formDate.formPropsObj">
        <template #otherItem="{ item, key }"><slot name="otherItem" :key="key" :item="item"></slot> </template>
        <template #otherFormItem="{ form }"><slot name="otherFormItem" :form="form"></slot> </template>
        <template #otherBtn="data"><slot name="otherBtn" :data="data"></slot></template>
        <template #recycleName="data"><slot name="recycleName" v-bind="data"></slot></template>
      </HfForm>
      <template #footer>
        <span class="dialog-footer" v-if="!dataed.superFormOpt?.noFooter">
          <el-button @click="dataed.open = false">取 消</el-button>
          <el-button v-if="!dataed.superFormOpt?.noSubmit" type="primary" @click="dataed.methods!.submitFormFn" :loading="dataed.subLoading" :disabled="dataed.subLoading">确 定</el-button>
          <slot name="otherFoterBtn"></slot>
        </span>
      </template>
    </el-dialog>
    <HfForm :ref="(el: any) => (dataed!.formRef = el)" v-else v-on="formDate.formEmitsObj" v-bind="formDate.formPropsObj">
      <template #otherItem="{ key, item }"><slot name="otherItem" :item="item" :key="key"></slot> </template>
      <template #otherFormItem="{ form }"><slot name="otherFormItem" :form="form"></slot> </template>
      <template #otherBtn="data">
        <div v-if="dataed.formType == 'pageForm'">
          <el-button type="primary" @click="dataed.methods!.submitFormFn" :loading="dataed.isSaving" :disabled="dataed.isSaving">{{ dataed.superFormOpt?.saveTxt || "保存" }} </el-button>
          <el-button v-if="!dataed.superFormOpt?.noBack" @click="dataed.methods!.backToListFn">返回</el-button>
          <el-button v-if="!dataed.superFormOpt?.noReset" type="primary" @click="dataed.methods!.resetFn"> 重置 </el-button>
        </div>
        <div v-if="dataed.formType == 'searchForm'">
          <el-button type="primary" @click="dataed.methods!.emitGetListFn" :loading="dataed.searching" :disabled="dataed.searching">查询</el-button>
          <el-button v-if="!dataed.superFormOpt?.noAdd" type="primary" @click="dataed.methods!.stoOpenFn"> {{ dataed.superFormOpt?.addBtnText || "新增" }} </el-button>
          <el-button v-if="!dataed.superFormOpt?.noReset" type="primary" @click="dataed.methods!.resetFn"> 重置 </el-button>
        </div>
        <slot name="otherBtn" :data="data"></slot>
      </template>
      <template #recycleName="data"><slot name="recycleName" v-bind="data"></slot></template>
    </HfForm>
  </div>
</template>

<script setup lang="ts">
import type { ISuP } from "../index";
import { formEmit, formData } from "@/components/HfForm/form";
import { supEmits, susEmits, sudEmits } from "./super-form";
import { getPageFormDataFn, getPageFormMethodsFn } from "./form-type/page-form";
import { getSearchFormDataFn, getSearchFormMethodsFn } from "./form-type/search-form";
import { getDiologFormDataFn, getDiologFormMethodsFn } from "./form-type/diolog-form";

const props = defineProps<ISuP>();
const emit = defineEmits([...formEmit, ...supEmits, ...susEmits, ...sudEmits]);
type ISuRGetFn = ISupRGetFn | ISusRGetFn | ISudRGetFn;
let dataed = ref<ISuRGetFn>();
let formDate = ref(new formData({ id: props.id, formOpt: props.formOpt, formParams: props.formParams }, emit));
function initialFn(): ISuRGetFn {
  formDate.value = new formData({ id: props.id, formOpt: props.formOpt, formParams: props.formParams }, emit);
  let data = {
    superFormOpt: props.superFormOpt,
    emit,
    formType: props.formType,
    formEmitsObj: formDate.value.formEmitsObj,
    formPropsObj: formDate.value.formPropsObj
  };
  if (data.formType == "diologForm" || data.formType == "drawForm") return getDiologFormMethodsFn(getDiologFormDataFn(data as ISudAGetData, dataed as Ref<ISudRGetData>) as Ref<ISudRGetFn>);
  if (data.formType == "searchForm") return getSearchFormMethodsFn(getSearchFormDataFn(data as ISusAGetData, dataed as Ref<ISusRGetData>) as Ref<ISusRGetFn>);
  return getPageFormMethodsFn(getPageFormDataFn(data as ISupAGetData, dataed as Ref<ISupRGetData>) as Ref<ISupRGetFn>);
}
initialFn();
const superFormEx: ISuEx = {
  dataed
};
defineExpose(superFormEx);
</script>

<style lang="scss"></style>
